package com.lvcoding.appclient.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    public RestTemplate restTemplate;

    @GetMapping("/index.html")
    public String index(String code, Model model) {
        if(code!=null) {
            MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
            map.add("code", code);
            map.add("client_id", "app");
            map.add("client_secret", "123");
            map.add("redirect_uri", "http://localhost:18082/index.html");
            map.add("grant_type", "authorization_code");
            Map<String, String> resp = restTemplate.postForObject("http://localhost:18080/oauth/token", map, Map.class);
            System.out.println("resp = " + resp);
            HttpHeaders headers = new HttpHeaders();
            headers.add("Authorization", "Bearer "+resp.get("access_token"));
            HttpEntity<?> httpEntity = new HttpEntity<>(headers);

            ResponseEntity<String> exchange = restTemplate.exchange("http://localhost:18081/admin", HttpMethod.GET, httpEntity, String.class);
            String msg = exchange.getBody();
            model.addAttribute("msg", msg);
        }

        return "index";
    }
}
